
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Thread.hpp"

using namespace ThreadMoudle;

int InitClient()
{
    int sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        std::cerr << "Create socket error" << std::endl;
        exit(1);
    }
    return sockfd;
}

void RecvMsg(int sockfd, std::string &serverip, in_port_t serverport, const std::string &name)
{
    while (true)
    {
        char buffer[1024];
        struct sockaddr_in peer;
        socklen_t len = sizeof peer;
        int n = recvfrom(sockfd, buffer, (sizeof buffer) - 1, 0, (struct sockaddr *)&peer, &len);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cerr << buffer << std::endl;
        }
        else if (n < 0)
        {
            std::cerr << "recvfrom() -> error, errno: " << errno << " -> " << strerror(errno) << std::endl;
            break;
        }
        else 
        {
            std::cerr << "recvfrom() None?" << std::endl;
            break;
        }
    }
}

void SendMsg(int sockfd, std::string &serverip, in_port_t serverport, const std::string &name)
{
    struct sockaddr_in server;
    memset(&server, 0, sizeof server);
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

    std::string cli_profix = name + "# ";

    while (true)
    {
        std::string line;
        std::cout << cli_profix;
        std::getline(std::cin, line);

        int n = sendto(sockfd, line.c_str(), line.size(), 0, (struct sockaddr *)&server, sizeof server);
        if (n < 0)
        {
            std::cout << "sendto() -> error, errno: " << errno << " -> " << strerror(errno) << std::endl;
            break;
        }
        else if (n == 0)
        {
            std::cout << "sendto() None?" << std::endl;
            // break;
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cout << "Usage: " << argv[0] << " Server-IP Server-Port" << std::endl;
        exit(0);
    }
    std::string serverip = argv[1];
    in_port_t serverport = std::stoi(argv[2]);

    int sockfd = InitClient();

    Thread recver("Recver-Thread", std::bind(&RecvMsg, sockfd, serverip, serverport, std::placeholders::_1));
    Thread sender("Sender-Thread", std::bind(&SendMsg, sockfd, serverip, serverport, std::placeholders::_1));

    recver.Start();
    sender.Start();

    recver.Join();
    sender.Join();

    ::close(sockfd);

    // while (true)
    // {
    //     std::string line;
    //     std::cout << "Please Enter # ";
    //     std::getline(std::cin, line);

    //     // 发消息
    //     // 得先知道发给谁
    //     int n = sendto(sockfd, line.c_str(), line.size(), 0, (struct sockaddr*)&server, sizeof server);
    //     if (n > 0)
    //     {
    //         // 收消息
    //         char buffer[1024];
    //         struct sockaddr_in temp;
    //         socklen_t len = sizeof temp;
    //         int m = recvfrom(sockfd, buffer, (sizeof buffer) -1, 0, (struct sockaddr*)&temp, &len);
    //         if (m > 0)
    //         {
    //             buffer[m] = 0;
    //             std::cout << buffer << std::endl;
    //         }
    //         else
    //         {
    //             break;
    //         }
    //     }
    //     else
    //     {
    //         break;
    //     }
    // }


    return 0;
}
